package site.zhouinfo.imageUtils;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.imageio.ImageIO;

public class ImgTest {
    public static void main(String[] args) throws Exception {
        //读背景文件
        Image src = ImageIO.read(new File("D:\\1.JPG"));
        BufferedImage image = new BufferedImage(src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        g.drawImage(src, 0, 0, null);

        String str = "为了保护世界和平";
        AttributedString ats = new AttributedString(str);
        Font f = new Font("", Font.PLAIN, 16);
        ats.addAttribute(TextAttribute.FONT, f, 0, str.length());
        AttributedCharacterIterator iter = ats.getIterator();

        //设置文字颜色
        /*
         * 参数值是颜色的10进制
         * 0是黑色
         * 65535是白色
         * 也可以写成0xFFFFFF 16进制的方式
         */
        g.setColor(new Color(0xFFFFFF));

        g.drawString(iter, 110, 60);
        g.dispose();

        //输出文件
        ImageIO.write(image, "png", new FileOutputStream(new File("D:\\2.JPG")));
    }
}
